import axios, {AxiosRequestConfig} from "axios"
import {getToken, setToken} from "../store";
import { bMessage } from "../components/popup/popup";
import { goLogin,goBack } from "../tools/go";
// export const baseUrl = "http://192.168.137.1:8080"


 // export const baseUrl = "http://192.168.29.3:8080"
export const baseUrl = "http://127.0.0.1:8080"
export const timeout = 500



export interface Page {
    page: Number,
    size: Number,
}

export interface HttpResponse {
    code: Number,
    msg: String,
    data?: any,
    timestamp: Number,
    now: String,
}

export interface HttpOptions extends AxiosRequestConfig<any>{
    msg?: boolean,
    token?: boolean,
    load?: boolean,
}

const http = axios.create({
    baseURL: baseUrl
})


http.interceptors.request.use(
    request => {

        if (getToken() == "") {
            goLogin()
        }

        if (request["token"] && getToken() !== "") {
            request.headers.auth = getToken()
        }

        return request;
    }
)

http.interceptors.response.use(
    response => {

        //无感刷新
        if (response.headers.token) {
            setToken(response.headers.token)
        }

        if (response.config["msg"]) {
            bMessage(response.data.msg, response.data.code == 200 ? "success" : "error")
        }

        if (response.data.code == 50000) {
            bMessage("你无访问的权限", "error")
            goBack()
        }

        if (response.data.code == 5000) {
            bMessage("登录已过期，请重新登录", "error")
            goLogin()
        }

        return response.data;
    },
    error => {
        // console.log(error.response.data)
        if (error.response.data.status == 50000) {
            bMessage("你无访问的权限", "error")
            goBack()
        }

        if (error.response.data.status == 5000) {
            bMessage("登录过期", "error")
            goLogin()
        }

        return Promise.reject(error);
    }
)



export default http